//
//  YSRegViewController.m
//  Yaoyle
//
//  Created by ZhaoXudong on 14-9-15.
//  Copyright (c) 2014年 ZhaoXudong. All rights reserved.
//

#import "YSRegViewController.h"
#import "YSUserModel.h"
#import "YSGlobal.h"
#import "JSONKit.h"
#import "NSData+AES256.h"
#import "YSWebViewController.h"

@interface YSRegViewController ()<UIAlertViewDelegate, UITextFieldDelegate> {
    __weak IBOutlet UITextField *phoneTextField;
    __weak IBOutlet UITextField *onePwdTextField;
    __weak IBOutlet UITextField *twoPwdTextField;
    
    __weak IBOutlet UIScrollView *contentView;
    NSDictionary *regOkDict;
    
    __weak IBOutlet UIImageView *checkBoxImage;
    BOOL isRead;
}

@end

@implementation YSRegViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        isRead = YES;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)readBtnClick:(id)sender {
    if (isRead) {
        isRead = NO;
        checkBoxImage.image = [UIImage imageNamed:@"checkBox_no"];
    } else {
        isRead = YES;
        checkBoxImage.image = [UIImage imageNamed:@"checkBox_yes"];
    }
}

- (IBAction)treatyBtnClick:(id)sender {
    YSWebViewController *webVC = [[YSWebViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:webVC];
    
    [self presentViewController:nav animated:YES completion:nil];
}

- (IBAction)regBtnClick:(id)sender {
    if (!isRead) {
        [self showMsgWithTitle:@"提示" msg:@"请仔细阅读比接受夜店骰王协议"];
        return;
    }
    
    NSString *phone = phoneTextField.text;
    NSString *onePwd = onePwdTextField.text;
    NSString *twoPwd = twoPwdTextField.text;
    
    if (phone.length == 0 || [YSUtility isBlankObject:phone]) {
        [self showMsgWithTitle:@"提示" msg:@"请输入要注册的手机号码"];
        return;
    }
    
    if (onePwd.length == 0 || [YSUtility isBlankObject:onePwd]) {
        [self showMsgWithTitle:@"提示" msg:@"请输入密码"];
        return;
    }
    
    if (twoPwd.length == 0 || [YSUtility isBlankObject:twoPwd]) {
        [self showMsgWithTitle:@"提示" msg:@"请确认密码"];
        return;
    }
    
    if (![onePwd isEqualToString:twoPwd]) {
        [self showMsgWithTitle:@"提示" msg:@"两次输入的密码不一致"];
        return;
    }
    [YSProgressHUD showHUDAddedTo:self.view];
    
    [[NetworkMgr sharedManager] asynchRequestWithFunId:eAPI_REG
                                              delegate:self
                                                params:@{@"mobile": phone,
                                                         @"password": onePwd}];
    
}

- (void)didSuccess:(NetworkMgr *)api userInfo:(NSDictionary *)info {
    [YSProgressHUD hideHUDForView:self.view];

    NSInteger funId = [info[kFUN_ID] integerValue];
    NSDictionary *re = info[API_Result_RE];
    if ([YSUtility isBlankObject:re]) {
        return;
    }
    
    re = [YSUtility returnDictionary:re];
    if (funId == eAPI_REG) {
        [self showMsgWithTitle:@"提示" msg:@"注册成功" delegate:self tag:10];
        regOkDict = @{RegPhoneKey: re[@"mobile"],
                      RegPwdKey: onePwdTextField.text,
                      RegScoreKey: re[@"scoreAmount"]};
    }
    
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if (textField == phoneTextField) {
        [contentView setContentOffset:CGPointMake(0, 10) animated:YES];
    } else if (textField == onePwdTextField) {
        [contentView setContentOffset:CGPointMake(0, 36) animated:YES];
    } else {
        [contentView setContentOffset:CGPointMake(0, 56) animated:YES];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [contentView setContentOffset:CGPointMake(0, 0) animated:YES];
    [textField resignFirstResponder];
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField == phoneTextField) {
        return [YSUtility validateNumber:string];
    }
    return YES;
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != alertView.cancelButtonIndex) {
        [self.navigationController popViewControllerAnimated:YES];
        if (self.block) {
            self.block(regOkDict);
        }
    }
}

@end
